﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SharingCenter.Web.Classes;
using SharingCenter.Web.Classes.Contact;

namespace SharingCenter.Web.Mod.orphans
{
    public partial class UserSearchResult : PageBase
    {
        protected string keyword;
        protected int curPage;
        protected int pageSize = 10;
        protected void Page_Init(object sender, EventArgs e)
        {
            //System.Threading.Thread.Sleep(5000);
            try
            {
                keyword = Request.Form["keyword"].Trim();
            }
            catch { msgError.Visible = true; }

            try
            {
                curPage = int.Parse(Request.Form["page"]);
            }
            catch { curPage = 1; }

            lvResult.ItemDataBound += new EventHandler<ListViewItemEventArgs>(lvResult_ItemDataBound);
        }

        void lvResult_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ProfileMembership p = (ProfileMembership)((ListViewDataItem)e.Item).DataItem;
            if (ContactOperations.AreUsersContacts(Profile.UserId, p.UserId))
            {
                MultiView mv = (MultiView)e.Item.FindControl("mvAddContact");
                mv.ActiveViewIndex = 1;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (keyword == string.Empty)
            {
                msgError.Visible = true;
            }
            LoadResult();
        }

        void LoadResult()
        {
            using (scDataContext cxt = new scDataContext())
            {
                var q = (from t in cxt.ProfileMemberships
                         where Convert.ToBoolean(t.IsSearchable) && Convert.ToBoolean(t.IsApproved) && !Convert.ToBoolean(t.IsLockedOut)
                         && t.UserId != Profile.UserId && (t.Email == keyword || t.NickName.Contains(keyword) || t.FullName.Contains(keyword))
                         select t).Skip((curPage - 1) * pageSize).Take(pageSize);
                lvResult.DataSource = q;
                lvResult.DataBind();
            }

        }
    }
}